
#include "system_init.h"
#include "Can_Refresh.h"



#define CAN_DEVICE_CHECK 0

tDeviceTimeOut DeviceTimeOut[Device_TIME_OUT_LOGIN_MAX_NUM];








//can监测设备注册与初始化
void CanDeviceLoginInit(void){

    memset(DeviceTimeOut,0x00,sizeof(DeviceTimeOut));
    //CanDeviceLogin(CanDevice_VCU,3000);
    CanDeviceLogin(CanDevice_MCU,3000);
    CanDeviceLogin(CanDevice_IP,3000);
    CanDeviceLogin(CanDevice_BMS,3000);
}
//can检测注册
int8_t CanDeviceLogin(eCanDevice canDevice,uint16_t outTime){
    if(canDevice>=Device_TIME_OUT_LOGIN_MAX_NUM)
        return -1; //组成数量越界
    DeviceTimeOut[canDevice].outTime = outTime;
    DeviceTimeOut[canDevice].outTimeCount = 0;
    return 0;
}
//can超时检测定时器挂载函数
//输入：挂载点的刷新周期时/ms
void CanDeviceTimeoutcheckMain(uint16_t timeSlice){
    uint8_t i=0;
    for (i = 0; i < Device_TIME_OUT_LOGIN_MAX_NUM; i++)
    {
        //fprintf(USART2_STREAM,"[%d]outTime:%d\r\n",i,DeviceTimeOut[i].outTime);
        if(DeviceTimeOut[i].outTime){
            if(DeviceTimeOut[i].outTimeCount >= DeviceTimeOut[i].outTime){
                //设备异常
                DeviceTimeOut[i].deviceState = FALSE;
                DeviceTimeOut[i].outTimeCount = DeviceTimeOut[i].outTime;
                switch (i)
                {
                case CanDevice_VCU:
                    //fprintf(USART2_STREAM,"warning:VCU life out\r\n");
                    break;
                case CanDevice_MCU:
                    SetMCUDataDefault_AC7();
                    //GPT_MotorDataNullify(&GBT_data_sum_handle);
                    //GPT_McuDataNullify(&GBT_data_sum_handle);
                    break;
                case CanDevice_IP:
                    SetIPDataDefault_AC7();
                    break;  
                case CanDevice_BMS:
                    SetBMSDataDefault_AC7();
                    //GPT_BtempDataNullify(&GBT_data_sum_handle);
                    //GPT_RechargesDataNullify(&GBT_data_sum_handle);
                    //GPT_BmsDataNullify(&GBT_data_sum_handle);
                    //fprintf(USART2_STREAM,"warning:BMS life out\r\n");
                    break;     
                case CanDevice_ABS:
                    //GPT_AbsEpsDataNullify(&GBT_data_sum_handle);
                    //fprintf(USART2_STREAM,"warning:ABS life out\r\n");
                    break;  
                case CanDevice_EPS:
                    //GPT_EpsEpsDataNullify(&GBT_data_sum_handle);
                    //fprintf(USART2_STREAM,"warning:EPS life out\r\n");
                    break;    
                case CanDevice_DCDC:
                    //fprintf(USART2_STREAM,"warning:DCDC life out\r\n");
                    SetDCDCDataDefault_AC7();
                    break;
                default:
                    break;
                }
            }else{
                DeviceTimeOut[i].outTimeCount+=timeSlice;
                DeviceTimeOut[i].deviceState = TRUE;
            }
        }
    }
}

//更新挂载ID刷新函数
int8_t CanDeviceLifeRefresh(eCanDevice canDevice ,uint8_t newLife){
    if(canDevice>=Device_TIME_OUT_LOGIN_MAX_NUM) return -1;

    if(DeviceTimeOut[canDevice].oldLifeTime != newLife){
        DeviceTimeOut[canDevice].oldLifeTime = newLife;
        DeviceTimeOut[canDevice].outTimeCount = 0;
    }else{
    #if CAN_DEVICE_CHECK
            switch (canDevice)
            {
            case CanDevice_VCU:
                fprintf(USART2_STREAM,"VCU dide\r\n");
                break;
            case CanDevice_MCU:
                fprintf(USART2_STREAM,"MCU dide\r\n");
                break;
            case CanDevice_IP:
                fprintf(USART2_STREAM,"IP dide\r\n");
                break;  
            case CanDevice_BMS:
                fprintf(USART2_STREAM,"BMS dide\r\n");
                break;                   
            default:
                break;
            }
    #endif
        DeviceTimeOut[canDevice].outTimeCount = DeviceTimeOut[canDevice].outTime;
    }
    return 0;
}

//检测设备状态是否有效
uint8_t IsOnlineCanDevice(eCanDevice canDevice){
    //fprintf(USART2_STREAM,"check id:%d,state is:\r\n",canDevice,DeviceTimeOut[canDevice].deviceState);
    if( DeviceTimeOut[canDevice].deviceState){
        return 1;
    }else{
        return 0;
    }
}

